home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / tk / xtk02.c < prev   
Encoding:
C/C++ Source or Header  |  1994-07-02  |  8.7 KB  |  364 lines

  1. /* $Id: xtk02.c,v 1.7 1994/07/01 20:44:35 mjl Exp $
  2.  * $Log: xtk02.c,v $
  3.  * Revision 1.7  1994/07/01  20:44:35  mjl
  4.  * Cruft elimination.
  5.  *
  6.  * Revision 1.6  1994/06/30  05:46:21  furnish
  7.  * Another plot command in tk02 which invokes a private tclMatrix
  8.  * extension for demo purposes.  xtk02.c adds a new tclMatrix subcommand
  9.  * "stuff", and tk02 exercises it.  Dumb, but shows how it all works.
  10.  *
  11.  * Revision 1.5  1994/06/23  22:40:29  mjl
  12.  * Fix to get prototype of pltkMain() correct, and some cleaning up.
  13.  *
  14.  * Revision 1.4  1994/06/16  19:30:25  mjl
  15.  * Changes to use pltkMain() for creating extended wish.  Should be more
  16.  * portable and robust than old method.
  17.  *
  18.  * Revision 1.3  1994/06/10  20:47:34  furnish
  19.  * Big time clean up.
  20.  *
  21.  * Revision 1.2  1994/05/26  22:38:08  mjl
  22.  * Added missing CVS Id and Log fields.
  23.  */
  24.  
  25. /* Before including plplot.h you must define TK to get all prototypes */
  26.  
  27. #define TK
  28. #include <plplot.h>
  29. #include <tk.h>
  30. #include <itcl.h>
  31. #include <math.h>
  32.  
  33. #include "tclMatrix.h"
  34.  
  35. /* A pithy little proc to show off how to install and use a tclMatrix
  36.    extension subcommand. This example is silly--only intended to show
  37.    how to do it.  What to do with it is your problem.  Could implement
  38.    subcommands for filling a matrix with special functions, performing
  39.    fft's, etc.
  40.    */
  41.  
  42. int stuff( tclMatrix *pm, Tcl_Interp *interp,
  43.        int argc, char *argv[] )
  44. {
  45.     int i;
  46.     float x, y;
  47.     printf( "made it into stuff, pm->n[0] = %d.\n", pm->n[0] );
  48.  
  49. /* Should check that matrix is right type, size, etc. */
  50.  
  51.     for( i = 0; i < pm->n[0]; i++ ) {
  52.     x = (float) i / pm->n[0];
  53.     y = sin( 6.28 * 4. * i / pm->n[0] ) * x * (1. - x) * 2 +
  54.         2. * x * (1. - x);
  55.     pm->fdata[i] = y;
  56.     }
  57.  
  58.     interp->result = "Things are cool in gumbyville.";
  59.     return TCL_OK;
  60. }
  61.  
  62.  
  63. /*----------------------------------------------------------------------*\
  64.  * main --
  65.  *
  66.  * Just a stub routine to call pltkMain.  The latter is nice to have
  67.  * when building extended wishes, since then you don't have to rely on
  68.  * sucking the Tk main out of libtk (which doesn't work correctly on all
  69.  * systems/compilers/linkers/etc).  Hopefully in the future Tk will
  70.  * supply a sufficiently capable tkMain() type function that can be used
  71.  * instead. 
  72. \*----------------------------------------------------------------------*/
  73.  
  74. int
  75. main(int argc, char **argv)
  76. {
  77.     exit(pltkMain(argc, argv));
  78. }
  79.  
  80. /*
  81.  *----------------------------------------------------------------------
  82.  *
  83.  * Tcl_AppInit --
  84.  *
  85.  *    This procedure performs application-specific initialization.
  86.  *    Most applications, especially those that incorporate additional
  87.  *    packages, will have their own version of this procedure.
  88.  *
  89.  * Results:
  90.  *    Returns a standard Tcl completion code, and leaves an error
  91.  *    message in interp->result if an error occurs.
  92.  *
  93.  * Side effects:
  94.  *    Depends on the startup script.
  95.  *
  96.  * Taken from tkAppInit.c --
  97.  *
  98.  * Copyright (c) 1993 The Regents of the University of California.
  99.  * All rights reserved.
  100.  *
  101.  * Permission is hereby granted, without written agreement and without
  102.  * license or royalty fees, to use, copy, modify, and distribute this
  103.  * software and its documentation for any purpose, provided that the
  104.  * above copyright notice and the following two paragraphs appear in
  105.  * all copies of this software.
  106.  * 
  107.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  108.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  109.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  110.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  111.  *
  112.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  113.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  114.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  115.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  116.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  117.  *----------------------------------------------------------------------
  118.  */
  119.  
  120. int   myplotCmd        (ClientData, Tcl_Interp *, int, char **);
  121.  
  122. int
  123. Tcl_AppInit(interp)
  124.     Tcl_Interp *interp;        /* Interpreter for application. */
  125. {
  126.     Tk_Window main;
  127.  
  128.     main = Tk_MainWindow(interp);
  129.  
  130.     /*
  131.      * Call the init procedures for included packages.  Each call should
  132.      * look like this:
  133.      *
  134.      * if (Mod_Init(interp) == TCL_ERROR) {
  135.      *     return TCL_ERROR;
  136.      * }
  137.      *
  138.      * where "Mod" is the name of the module.
  139.      */
  140.  
  141.     if (Tcl_Init(interp) == TCL_ERROR) {
  142.     return TCL_ERROR;
  143.     }
  144.     if (Tk_Init(interp) == TCL_ERROR) {
  145.     return TCL_ERROR;
  146.     }
  147.     if (Itcl_Init(interp) == TCL_ERROR) {
  148.     return TCL_ERROR;
  149.     }
  150.     if (Pltk_Init(interp) == TCL_ERROR) {
  151.     return TCL_ERROR;
  152.     }
  153.  
  154.     /*
  155.      * Call Tcl_CreateCommand for application-specific commands, if
  156.      * they weren't already created by the init procedures called above.
  157.      */
  158.  
  159.     Tcl_CreateCommand(interp, "myplot", myplotCmd,
  160.                       (ClientData) main, (void (*)(ClientData)) NULL);
  161.  
  162.     Tcl_MatrixInstallXtnsn( "stuff", stuff );
  163.  
  164.     return TCL_OK;
  165. }
  166.  
  167. void myplot1();
  168. void myplot2();
  169. void myplot3();
  170. void myplot4();
  171.  
  172. /* Plots several simple functions */
  173. /* Note the compiler should automatically convert all non-pointer arguments
  174.    to satisfy the prototype, but some have problems with constants. */
  175.  
  176. static PLFLT x[101], y[101];
  177. static PLFLT xscale, yscale, xoff, yoff, xs[6], ys[6];
  178. static PLINT space0 = 0, mark0 = 0, space1 = 1500, mark1 = 1500;
  179.  
  180. void plot1(void);
  181. void plot2(void);
  182. void plot3(void);
  183.  
  184. /* This has been superceeded by Tcl code in tk02 */
  185.  
  186. void myplot1()
  187. {
  188. /* Set up the data */
  189. /* Original case */
  190.  
  191.     xscale = 6.;
  192.     yscale = 1.;
  193.     xoff = 0.;
  194.     yoff = 0.;
  195.  
  196. /* Do a plot */
  197.  
  198.     plot1();
  199. }
  200.  
  201. void myplot2()
  202. {
  203.     PLINT digmax;
  204.  
  205. /* Set up the data */
  206.  
  207.     xscale = 1.;
  208.     yscale = 0.0014;
  209.     yoff = 0.0185;
  210.  
  211. /* Do a plot */
  212.  
  213.     digmax = 5;
  214.     plsyax(digmax, 0);
  215.     plot1();
  216. }
  217.  
  218. void myplot3()
  219. {
  220.     plot2();
  221. }
  222.  
  223. void myplot4()
  224. {
  225.     plot3();
  226. }
  227.  
  228.  /* =============================================================== */
  229.  
  230. void
  231. plot1(void)
  232. {
  233.     int i;
  234.     PLFLT xmin, xmax, ymin, ymax;
  235.  
  236.     for (i = 0; i < 60; i++) {
  237.     x[i] = xoff + xscale * (i + 1) / 60.0;
  238.     y[i] = yoff + yscale * pow(x[i], 2.);
  239.     }
  240.  
  241.     xmin = x[0];
  242.     xmax = x[59];
  243.     ymin = y[0];
  244.     ymax = y[59];
  245.  
  246.     for (i = 0; i < 6; i++) {
  247.     xs[i] = x[i * 10 + 3];
  248.     ys[i] = y[i * 10 + 3];
  249.     }
  250.  
  251. /* Set up the viewport and window using PLENV. The range in X is */
  252. /* 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes are */
  253. /* scaled separately (just = 0), and we just draw a labelled */
  254. /* box (axis = 0). */
  255.  
  256.     plcol(1);
  257.     plenv(xmin, xmax, ymin, ymax, 0, 0);
  258.     plcol(6);
  259.     pllab("(x)", "(y)", "#frPLplot Example 1 - y=x#u2");
  260.  
  261. /* Plot the data points */
  262.  
  263.     plcol(9);
  264.     plpoin(6, xs, ys, 9);
  265.  
  266. /* Draw the line through the data */
  267.  
  268.     plcol(4);
  269.     plline(60, x, y);
  270. }
  271.  
  272.  /* =============================================================== */
  273.  
  274. void
  275. plot2(void)
  276. {
  277.     int i;
  278.  
  279. /* Set up the viewport and window using PLENV. The range in X is -2.0 to
  280.    10.0, and the range in Y is -0.4 to 2.0. The axes are scaled separately
  281.    (just = 0), and we draw a box with axes (axis = 1). */
  282.  
  283.     plcol(1);
  284.     plenv(-2.0, 10.0, -0.4, 1.2, 0, 1);
  285.     plcol(2);
  286.     pllab("(x)", "sin(x)/x", "#frPLplot Example 1 - Sinc Function");
  287.  
  288. /* Fill up the arrays */
  289.  
  290.     for (i = 0; i < 100; i++) {
  291.     x[i] = (i - 19.0) / 6.0;
  292.     y[i] = 1.0;
  293.     if (x[i] != 0.0)
  294.         y[i] = sin(x[i]) / x[i];
  295.     }
  296.  
  297. /* Draw the line */
  298.  
  299.     plcol(3);
  300.     plline(100, x, y);
  301.  
  302. }
  303.  
  304.  /* =============================================================== */
  305.  
  306. void
  307. plot3(void)
  308. {
  309.     int i;
  310.  
  311. /* For the final graph we wish to override the default tick intervals, and
  312.    so do not use PLENV */
  313.  
  314.     pladv(0);
  315.  
  316. /* Use standard viewport, and define X range from 0 to 360 degrees, Y range
  317.        from -1.2 to 1.2. */
  318.  
  319.     plvsta();
  320.     plwind(0.0, 360.0, -1.2, 1.2);
  321.  
  322. /* Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y. */
  323.  
  324.     plcol(1);
  325.     plbox("bcnst", 60.0, 2, "bcnstv", 0.2, 2);
  326.  
  327. /* Superimpose a dashed line grid, with 1.5 mm marks and spaces. plstyl
  328.    expects a pointer!! */
  329.  
  330.     plstyl(1, &mark1, &space1);
  331.     plcol(2);
  332.     plbox("g", 30.0, 0, "g", 0.2, 0);
  333.     plstyl(0, &mark0, &space0);
  334.  
  335.     plcol(3);
  336.     pllab("Angle (degrees)", "sine", "#frPLplot Example 1 - Sine function");
  337.  
  338.     for (i = 0; i < 101; i++) {
  339.     x[i] = 3.6 * i;
  340.     y[i] = sin(x[i] * 3.141592654 / 180.0);
  341.     }
  342.  
  343.     plcol(4);
  344.     plline(101, x, y);
  345. }
  346.  
  347. int   myplotCmd( ClientData cd, Tcl_Interp *interp, int argc, char **argv )
  348. {
  349.     if (!strcmp(argv[1],"1"))
  350.       myplot1();
  351.  
  352.     if (!strcmp(argv[1],"2"))
  353.       myplot2();
  354.  
  355.     if (!strcmp(argv[1],"3"))
  356.       myplot3();
  357.  
  358.     if (!strcmp(argv[1],"4"))
  359.       myplot4();
  360.  
  361.     plflush();
  362.     return TCL_OK;
  363. }
  364.